package me.jiatao.paging3

import android.app.Application
import androidx.room.Room
import me.jiatao.paging3.room.AppDatabase
import kotlin.properties.Delegates

class MyApplication : Application() {
    companion object {
        /**
         * 引用Application对象的单例
         */
        private var instance: MyApplication by Delegates.notNull()

        private val database: AppDatabase by lazy {
            Room.databaseBuilder(instance, AppDatabase::class.java, "room.db")
                .fallbackToDestructiveMigration()
                .build()
        }

        /**
         * 创建数据库对象单例
         */
        fun getDB(): AppDatabase {
            return database
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
    }
}